package com.kotlin.demo.tools

import com.kotlin.demo.constant.Constants
import com.kotlin.demo.net.Iciba
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

/**
 * 翻译工具类
 */
object TranslateUtils {
    /**
     * 翻译所有语言
     */
    suspend fun getAllTranslate(translateText: String): String {
        val transManager = Iciba()
        val fromLanguage = "zh"
        val map = hashMapOf<String,String>()
        Constants.TRANSLATE_LIST_TAG.forEach {
            if (it.shortName != fromLanguage) {
                val translateResult = transManager.icibaApi(translateText, fromLanguage, it.apiName)
                map[it.shortName] = translateResult
                println("translateResult:$translateResult")
            }
        }
        map[fromLanguage] = translateText.trim()
        val json = Json{prettyPrint  = true}
        return json.encodeToString(map)
    }

}